﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using JChatCore.User;
using JChatData.UserMap;
using JChatData.Helper;
using jchat_encrypt;

namespace JChatSerVices.UserService
{
    public partial class UserService:IUserService
    {
        DBConectString _db;
        GetConnect.GetFileConnect _get;
        jchat_encrypt.jchat_encrypt _jchatencrypt;
        public UserService()
        {
            _get = new GetConnect.GetFileConnect();
            string file = _get.GetFile("conn");
            _db = new DBConectString(file);
            _jchatencrypt = new jchat_encrypt.jchat_encrypt();
        }
        //TO DO: remove special charaters
        public string RemoveSpecialCharater(string s)
        {
            string str = "";
            foreach(char c in s)
            {
                if((c>='A'&&c<='Z')||(c>='a'&&c<='z')||(c>='0'&&c<='9')||(c=='.')||(c=='_'))
                    str+=c;
            }
            return str;
        }
        public bool Login(string _username, string _password)
        {
            string _uname = RemoveSpecialCharater(_username);
            string _pass = RemoveSpecialCharater(_password);
            string mahoa = _jchatencrypt.Encrypt("absoft00166^^()", _pass);
            try
            {
                var _user = (from u in _db._context.JChat_User
                             where u.UserName == _uname && u.Password.Substring(0, u.Password.Length - 10) == mahoa
                             select u).ToList();
                if (_user != null)
                    return true;
                return false;
            }
            catch
            {
                return false;
            }
        }
        #region
        //TO DO: GET DATA
        public List<User> GetAllUser()
        {
            var _user = (from u in _db._context.JChat_User
                         select u).ToList();
            return _user;
        }
        public List<User> GetUserById(Guid _id)
        {
            var _user = (from u in _db._context.JChat_User
                         where u.UserID == _id
                         select u).ToList();
            return _user;
        }
        #endregion
        #region
        public string RandomString()
        {
            string s = "";
            Random rand = new Random();
            for (int i = 0; i < 10; i++)
            {
                if (i == 0 || i == 3 || i == 6 || i == 9)
                {
                    s += (char)(97 + rand.Next(0, 26));
                }
                else
                {
                    s += rand.Next(0, 9);
                }
            }
            return s;
        }
        //TO DO: add, update, delete
        public bool AddUser(User _user)
        {
            try
            {
                var _u = (from u in _db._context.JChat_User
                          where u.UserName == _user.UserName
                          select u).ToList();
                if (_u != null)
                    return false;
                string mahoa = _jchatencrypt.Encrypt("absoft00166^^()", _user.Password);
                _user.Password = mahoa + RandomString();
                _db._context.JChat_User.AddObject(_user);
                _db._context.SaveChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }
        public bool UpdateUser(Guid _id, User _usertable)
        {
            try
            {
                var _user = (from u in _db._context.JChat_User
                             where u.UserID == _id
                             select u).SingleOrDefault();
                _user.RealName = _usertable.RealName;
                _user.UserName = _usertable.UserName;
                string mahoa = _jchatencrypt.Encrypt("absoft00166^^()", _usertable.Password);
                _user.Password = mahoa + RandomString();
                _user.PasswordTemp = _usertable.PasswordTemp;
                _user.Email = _usertable.Email;
                _user.Address = _usertable.Address;
                _user.CityID = _usertable.CityID;
                _user.CountryID = _usertable.CountryID;
                _user.Status = _usertable.Status;
                _user.Sex = _usertable.Sex;
                _user.BirthDay = _usertable.BirthDay;
                _user.Avatar = _usertable.Avatar;
                _user.CreateDate = _usertable.CreateDate;
                _user.LastLoginDate = _usertable.LastLoginDate;
                _user.LastActivityDate = _usertable.LastActivityDate;
                _user.OtherInfo = _usertable.OtherInfo;
                _user.Active = _usertable.Active;
                _db._context.SaveChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }
        public bool DeleteUser(Guid _id)
        {
            try
            {
                var _user = (from u in _db._context.JChat_User
                             where u.UserID == _id
                             select u).SingleOrDefault();
                _db._context.DeleteObject(_user);
                return true;
            }
            catch
            {
                return false;
            }
        }
        #endregion
    }
}
